<?php
namespace backend\controllers;

use Yii;
use yii\web\AdminController;
use backend\models\UploadForm;
use backend\models\GoodsCategory;
use backend\models\HotSaleConfig;
use backend\models\BannerConfig;

class ActivityController extends AdminController
{
    public $layout = false;
    //业务关键字
    public $service = 'Activity';
    
    public $type = 3;
    
    public function actionIndex()
    {
        $model = new UploadForm();
        return $this->render('index',['model'=>$model]);
    }
    
    public function actionGetActivity(){
        $data = Yii::$app->request->post();
        $type = $this->type;
        $query = HotSaleConfig::find()->select('id,sort,platform,use_flg')->where('type='.$type)->orderBy('use_flg desc,edit_time desc');
        $count = $query->count();
        $list = $query->offset($data['start'])->limit($data['length'])->asArray()->all();
        $result_val = [];
        if (count($list) > 0){
            $id = array_column($list, 'id');
            $banner_config = BannerConfig::find()->where(['in','group_id',$id])->asArray()->all();
            $banner_config_arr = [];
            if (is_array($banner_config) && count($banner_config) > 0)foreach ($banner_config as $banner){
                $banner_config_arr[$banner['group_id']][] = $banner;
            }
            foreach ($list as $val){
                $val['banner_config'] = $banner_config_arr[$val['id']];
                $result_val[] = $val;
            }
        }
        $result['draw'] = intval($data['draw']);
        $result['recordsTotal'] = intval($count);
        $result['recordsFiltered'] = intval($count);
        $result['data'] = $result_val;
        return \yii\helpers\Json::encode($result);
    }
    
    public function actionChangeActivitySort(){
        $model = new HotSaleConfig();
        $data = Yii::$app->request->post();
        $id = $data['id'];
        $sort = $data['sort'];
        $data_new = [
            'sort'=>$sort,
            'editor'=>$model->getCreator(),
            'edit_time'=>date('Y-m-d H:i:s')
        ];
        $ret = $model::updateAll($data_new,['id'=>$id]);
        if(!$ret){
            throw new \Exception('操作失败');
        }
        $this->retJson('操作成功',200);
    }
    
    public function actionChangeActivityStatus(){
        $model = new HotSaleConfig();
        $data = Yii::$app->request->post();
        $id = $data['id'];
        $use_flg = $data['use_flg'];
        $data_new = [
            'use_flg'=>$use_flg,
            'editor'=>$model->getCreator(),
            'edit_time'=>date('Y-m-d H:i:s')
        ];
        $ret = $model::updateAll($data_new,['id'=>$id]);
        if(!$ret){
            throw new \Exception('操作失败');
        }
        $this->retJson('操作成功',200);
    }
    
    public function actionActivityDetail(){
        $data = Yii::$app->request->post();
        $id = $data['id'];
        $activity = HotSaleConfig::find()->select(
            'id,sort,platform,use_flg,type'
        )->where('id=:id',[':id'=>$id])->asArray()->one();
        if (count($activity) > 0){
            $banner_config = BannerConfig::find()->select(
                'id,picture_name,picture_url,product_url')->where('group_id='.$id)->orderBy('sort asc')->asArray()->one();
            $activity['banner_config'] = $banner_config;
        }
        $this->retJson($activity,200);
    }
    
    public function actionChangeActivity(){
        $model = new HotSaleConfig();
        $data = Yii::$app->request->post();
        $transaction = Yii::$app->db->beginTransaction();
        try {
            $oper = $model->getCreator();
            $oper_time = date('Y-m-d H:i:s');
            $hot_sale_id = $data['hot_sale_id'];
            $use_flg = $data['use_flg'];
            $platform = $data['platform'];
            $picture_name = $data['picture_name'];
            $sort = $data['sort'];
            $picture_url = $data['picture_url'];
            $product_url = $data['product_url'];
            $type = $this->type;
            if (!$platform){
                throw new \Exception('请选择平台');
            }
            if (!$picture_name){
                throw new \Exception('请填写活动图片名称');
            }
            if (!$sort){
                throw new \Exception('请填写活动排序编号');
            }
            if (!$picture_url){
                throw new \Exception('请上传一张商品图片');
            }
            if (!$product_url){
                throw new \Exception('请填写商品链接');
            }
            $banner = new BannerConfig();
            $banner_id = '';
            if ($hot_sale_id){
                $model = HotSaleConfig::findOne($hot_sale_id);
                $model->editor = $oper;
                $model->edit_time = $oper_time;
                $where_banner['group_id'] = $hot_sale_id;
                $banner = BannerConfig::find()->where($where_banner)->one();
                $banner_id = $banner->id;
            }else{
                $model->category_id = 0;
                $model->creator = $oper;
                $model->create_time = $oper_time;
                $model->type = $type;
            }
            $model->platform = $platform;
            $model->sort = $sort;
            $model->use_flg = $use_flg;
            $model->save();
            $hot_sale_new_id = $model->attributes['id'];
            if (!$hot_sale_new_id){
                throw new \Exception('活动维护失败');
            }
            $sort = 1;
            if ($banner_id){
                $banner->editor = $oper;
                $banner->edit_time = $oper_time;
            }else{
                $banner->creator = $oper;
                $banner->create_time = $oper_time;
            }
            $banner->group_id = $hot_sale_new_id;
            $banner->product_url = $product_url;
            $banner->picture_name = $picture_name;
            $banner->sort = $sort;
            $banner->picture_url = $picture_url;
            $banner->save();
            $banner_id = $banner->attributes['id'];
            if (!$banner_id){
                throw new \Exception('热销商品明细操作失败');
            }
            $transaction->commit();
            $this->retJson('编辑成功',200);
        } catch (\Exception $e) {
            $transaction->rollBack();
            //add by allen.qiang 20180112 异常处理，日志优化 start
            error_log('error->' . __METHOD__ . ' error:' . $e->getMessage());
            //add by allen.qiang 20180112 异常处理，日志优化 end
            $this->retJson($e->getMessage(),500);
        }
    }
}